<?php
/**
* File that sets view of edit busstops page.
* @package busmap
*/
?>
<?php if(count($this->entries)){ ?>
	<h3>Bussipeatused</h3>
	<table class="data-listing">
		<tr>
			<th>Nimi</th>
			<th>Kordinaadid</th>
			<th>Läbivad liinid</th>
			<th class="button">Tegevused</th>
		</tr>
		<tr>
			<td colspan="4">
				<?php foreach ($this->entries as $entry){
				if(array_key_exists('id', $_POST) && $_POST['id'] == $entry->id){
					
					if(count($this->changeBusstopFormErrors)){
						$errorsTxt = '';
						foreach($this->changeBusstopFormErrors as $error){
							$errorsTxt .= '<div class="withErrors">'.array_pop($error).'</div>';
						}
						echo $errorsTxt;
					}
					if(array_key_exists('delete', $_POST)){
						$this->changeBusstopForm = new Application_Form_Admin_editBusstop();
						$this->changeBusstopForm->populate($entry->toArray());
						$this->changeBusstopForm->askDeleteConfirmation();
					}
					$busstopsForm = $this->changeBusstopForm;
				}else{
					$busstopsForm = new Application_Form_Admin_editBusstop();
					$busstopsForm->populate($entry->toArray());
				}

				$buslines = array();
				$usedBuslines = array();
				$buslineToBusstopMapper = new Application_Model_BusLineToBusStopMapper();
				$buslinesToBusstop = $buslineToBusstopMapper->getDbTable()->fetchAll(array('busStop_id = ? '=>$entry->id));
				foreach($buslinesToBusstop as $buslineToBusstop){
					$buslineMapper = new Application_Model_BusLineMapper();
					$busline = new Application_Model_BusLine();
					$buslineMapper->find($buslineToBusstop['busLine_id'], $busline);
					if($busline->id){
						$colorMapper = new Application_Model_ColorMapper();
						$color = new Application_Model_Color();
						$colorMapper->find($busline->colorId, $color);
						if(!in_array($busline->id, $usedBuslines)){
							$usedBuslines[] = $busline->id;
							$buslines[] = array($busline, $color);
						}
					}

				}
				$busstopsForm->populateWithBusLines($buslines);

				echo $busstopsForm.'<div style="clear:both;"></div>';
				}
				?>
			</td>
		</tr>
	</table>
<?php }else{ ?>
	Hetkel bussipeatused puuduvad andmebaasis.
<?php } ?>
<h4>Lisa uus bussipeatus</h4>
<?php
if(count($this->errors)){
	$errorsTxt = '';
	foreach($this->errors as $error){
		$errorsTxt .= '<div class="withErrors">'.array_pop($error).'</div>';
	}
	echo ($errorsTxt)?$errorsTxt:'';
}
?>
<table class="data-listing">
	<tr>
		<th>Bussipeatuse nimi</th>
		<th>X | Y</th>
		<th class="button">Tegevus</th>
	</tr>
	<tr>
		<td colspan="3">
			<?php
				echo $this->insertBusstopForm;
			?>
		</td>
	</tr>
</table>